package d2_UDP;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Server {
    public static void main(String[] args) throws Exception {
        System.out.println("Server starting...");
        //服务端创建
        DatagramSocket socket = new DatagramSocket(7894);
        //创建接受数据包
        byte[] buf = new byte[1024*64];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        //接收数据
        socket.receive(packet);
        int len = packet.getLength();
        String rs = new String(buf,0,len);
        System.out.println(rs);

        String hs=  packet.getAddress().getHostAddress();
        int pt= packet.getPort();
        System.out.println("Server received: "+hs);
        System.out.println("Server received: "+pt);

        socket.close();
    }
}
